import service_filter from '@/service/filter.js'

export default {
	namespaced: true,
	
	state:{
		filtrate_style: uni.getStorageSync("filtrate_style") || null,      // 设计风格
		filtrate_offer: uni.getStorageSync("filtrate_offer") || null,      // 设计报价
		filtrate_area: uni.getStorageSync("filtrate_area") || null,        // 面积
		filtrate_budget: uni.getStorageSync("filtrate_budget") || null,    // 预算
		filtrate_employ: uni.getStorageSync("filtrate_employ") || null,    // 从业年份
		filtrate_service: uni.getStorageSync("filtrate_service") || null,  // 服务类型	
	},
	
	mutations: {
		// 设置-设计风格
		["SET_STYLE"] (state, payload){ state.filtrate_style = payload; uni.setStorageSync("filtrate_style", payload) },
		// 设置-设计报价
		["SET_OFFER"] (state, payload){ state.filtrate_offer = payload; uni.setStorageSync("filtrate_offer", payload) },
		// 设置-面积
		["SET_AREA"] (state, payload){ state.filtrate_area = payload; uni.setStorageSync("filtrate_area", payload) },
		// 设置-预算
		["SET_BUDGET"] (state, payload){ state.filtrate_budget = payload; uni.setStorageSync("filtrate_budget", payload) },
		// 设置-从业年份
		["SET_EMPLOY"] (state, payload){ state.filtrate_employ = payload; uni.setStorageSync("filtrate_employ", payload) },
		// 设置-服务类型
		["SET_SERVICE"] (state, payload){ state.filtrate_service = payload; uni.setStorageSync("filtrate_service", payload) },
	},
	
	actions: {
		getFilter({ commit }, ) {
			return new Promise((resolve, reject) => {
				service_filter.getOptions()
					.then(res => {
						if (res.code == 1) {
							let data = res.data
							
							// 风格
							if (data.design_style) {
								let style = data.design_style
								let nStyle = [{ select: false, value: null, label: '全部' }]
								for (let i = 0; i < style.length; i++) {
									nStyle.push({ select: false, value: style[i].id, label: style[i].name })
								}
								commit('SET_STYLE', nStyle)
							}
							
							// 报价
							if (data.offer_section) {
								let offer = data.offer_section
								let nOffer = [{ value: null, label: '全部' }]
								for (let i = 0; i < offer.length; i++) {
									nOffer.push({ value: offer[i].value, label: offer[i].name })
								}
								commit('SET_OFFER', nOffer)
							}
							
							// 面积
							if (data.area_section) {
								let area = data.area_section
								let nArea = [{ value: null, label: '全部' }]
								for (let i = 0; i < area.length; i++) {
									nArea.push({ value: area[i].value, label: area[i].name })
								}
								commit('SET_AREA', nArea)
							}
							
							// 预算
							if (data.budget_section) {
								let budget = data.budget_section
								let nBudget = [{ value: null, label: '全部' }]
								for (let i = 0; i < budget.length; i++) {
									nBudget.push({ value: budget[i].value, label: budget[i].name })
								}
								commit('SET_BUDGET', nBudget)
							}
							
							// 从业年份
							if (data.employ_info) {
								commit('SET_EMPLOY', data.employ_info)
							}
							
							// 服务类型
							if (data.service_type) {
								commit('SET_SERVICE', data.service_type)
							}
						}
						
						resolve(res)
					})
					.catch(err => {
						reject(err)
					})
			})
		}
	},
	
}